package org.example.classloader.ASM;

import jdk.internal.org.objectweb.asm.MethodVisitor;
import jdk.internal.org.objectweb.asm.Opcodes;

public class ModifyMethodVisitor extends MethodVisitor {

    public ModifyMethodVisitor(MethodVisitor methodVisitor) {
        super(Opcodes.ASM5, methodVisitor);
    }

    public void visitCode() {

        super.visitMethodInsn(Opcodes.INVOKESTATIC,
                "com/crazymakercircle/classLoaderDemo/AOP/Transaction",
                "beginTransaction",
                "()V",
                false);
        super.visitCode();
    }

    public void visitInsn(int opcode) {

        /*
         * 方法return之前，植入代码
         */
        if (opcode == Opcodes.RETURN || opcode == Opcodes.ARETURN) {
            super.visitMethodInsn(Opcodes.INVOKESTATIC,
                    "com/crazymakercircle/classLoaderDemo/AOP/Transaction",
                    "commit",
                    "()V",
                    false);
        }

        super.visitInsn(opcode);
    }
}